package controller;

import java.sql.ResultSet;
import misc.Configurations;
import misc.Logger;
import data.DatabaseGateway;
import data.File;

public class DeleteController {

    public File retrieveFile(String hashedcode)
    {
        try
        {
            String query = 
                    "SELECT * FROM FILES WHERE HashedCode='"+hashedcode+"'";

            DatabaseGateway gw = new DatabaseGateway();
            gw.openConnection(Configurations.User,Configurations.Password,
                                    Configurations.DatabaseName);
            int numRows=gw.executeCount(query);

            if(numRows<=0)
            return null;

            ResultSet rs = gw.executeQuery(query);

            File new_file = new File();
            rs.next();//Move into first row
            new_file.setFileID(rs.getLong("FileID"));
            new_file.setFilepath(rs.getString("FilePath"));
            new_file.setDeleted(rs.getBoolean("isDeleted"));
            new_file.setName(rs.getString("OriginalName"));
            new_file.setSize(rs.getLong("Size"));
            new_file.setHashedCode(rs.getString("HashedCode"));

            gw.closeConnection();

            return new_file;
        }
        catch(Exception e)
        {
            Logger.write(e.getMessage(),Logger.LogType.error);
        }

        return null;
    }
}
